<html>
<head>
    <title>添加</title>
    <meta charset="UTF-8">
</head>
<link rel="stylesheet" href="/static/layui/css/layui.css" />
<body>

<form class="layui-form"  action="" lay-filter="edit">
    <div class="layui-form-item">
        <div class="layui-input-inline layui-col-md1">
            <input type="hidden" name="uid" id="uid" lay-verify="required" autocomplete="off"  class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">身份：</label>
        <div class="layui-input-block">
            <select name="rid" id="rid" lay-verify="required">
                <option value=""></option>
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">账户：</label>
        <div class="layui-input-inline layui-col-md3">
            <input type="text" name="uusername" id="uusername" lay-verify="required" autocomplete="off" placeholder="..." class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">密码：</label>
        <div class="layui-input-inline layui-col-md4">
            <input type="text" name="upassword" id="upassword"  required  lay-verify="required" placeholder="..." autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">姓名：</label>
        <div class="layui-input-inline layui-col-md5">
            <input type="text" name="uname" id="uname" lay-verify="required" autocomplete="off" placeholder="..." class="layui-input">
        </div>
    </div>
    </div><div class="layui-form-item">
    <label class="layui-form-label">年龄：</label>
    <div class="layui-input-inline layui-col-md6">
        <input type="text" name="uage" id="uage" lay-verify="required" placeholder="..." autocomplete="off" class="layui-input">
    </div>
</div>
    <div class="layui-form-item">
        <label class="layui-form-label">电话：</label>
        <div class="layui-input-inline layui-col-md7">
            <input type="text" name="utel" id="utel"  required  lay-verify="required" placeholder="..." autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">状态：</label>
        <div class="layui-input-inline layui-col-md8">
            <input type="text" name="ustatus" id="ustatus"  required  lay-verify="required" placeholder="..." autocomplete="off" class="layui-input">
        </div>
    </div>


    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="formDemo">立即提交</button>
        </div>
    </div>
</form>
</body>

<script src="/static/js/system.js"></script>
<script src="/static/js/jquery-3.3.1.js"></script>
<script src="/static/layui/layui.js"></script>
<script>
    //Demo
    layui.use(['form','laydate'], function(){
        var form = layui.form;
        $.ajax({
            url: "/UserServlet?method=queryRoleAll",
            type:"post",
            dataType: "json",
            success:function (rs) {
                if (rs.code==0){
                    $.each(rs.data, function (index, item) {
                        // 追加option节点
                        //item.name是option里的文本值
                        //item.id是option的value值
                        $("#rid").append(new Option(item.roleType, item.id));
                    });
                }
            }
        });
        $.ajax({
            url:'/UserServlet?method=queryOne',
            dataType:"json",
            data:{"id":getQueryString("id")},
            success:function (da) {
                console.log(da);
                if (da.code == 0) {
                    // form.val("edit",da.data);
                    $.each(da.data,function (index,i) {
                        $('#uid').val(i.uid);
                        // $('#rid').val(i.rid);
                        $("#rid").find("option[value="+i.rid+"]").prop("selected",true);
                        $('#uusername').val(i.uusername);
                        $('#upassword').val(i.upassword);
                        $("#uname").val(i.uname);
                        $('#uage').val(i.uage);
                        $('#utel').val(i.utel);
                        $("#ustatus").val(i.ustatus);
                    });
                    //重新渲染下拉框
                    form.render('select');
                } else {
                    layer.msg("查询数据失败！");
                }
            }
        });

        //监听提交 15039939677
        form.on('submit(formDemo)', function(data){
            var data1 = form.val("edit");
            console.log(data1);
            $.ajax({
                url:'/UserServlet?method=update',
                type:"post",
                data:data1,
                dataType:"json",
                success:function (da) {
                    if (da.code == 0) {
                        console.log(da);
                        layer.alert("修改成功！", function () {
                            // window.parent.location.reload();//刷新父页面
                            // parent.layer.close(layer.index);//关闭弹出层
                            parent.layer.closeAll();
                        });
                    } else {
                        layer.msg("修改失败！");
                    }
                }
            });
            return false;
        });
    });
</script>
</html>
